""" command-line util for running this
    
    To document an entire package:
        python apyidoc package
    
    To document a stand-alone module:
        python apyidoc module
    
    To document a single module of a package:
        python apydoc package module
        
    where "package" and "module" are absolute or relative paths to the package
"""

import sys, os

if __name__ == "__main__":

    # add ourselves to the path
    parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    sys.path.append(parent_dir)
    print 'running %s' % __name__
    from apyidoc.converter import convertmodule
    from apyidoc.templates import templates
    
    # arg1, arg2
    # if arg2, then arg1 is packagepath and arg2 is a dotted module
    # else:
    # if arg1 is a dir, walk over all files
    # otherwise just document that
    
    if len(sys.argv) == 3:
        pkgpth = sys.argv[1]
        tgtpth = sys.argv[2]
        convertmodule(tgtpth, templates, pkgpth)
    elif len(sys.argv) == 2:
        pkgpth = sys.argv[1]
        if os.path.isdir(pkgpth):
            for dirpath, dirnames, filenames in os.walk(pkgpth, topdown=True):
                for f in filenames:
                    if f.endswith('.py'):
                        tgtpth = os.path.join(dirpath, f)
                        convertmodule(tgtpth, templates, pkgpth)
        else:
            tgtpth = sys.argv[1]
            convertmodule(tgtpth, templates)